package org.hu.vhr.controller.Sys.basic;

import org.hu.vhr.model.Menu;
import org.hu.vhr.model.RespBean;
import org.hu.vhr.model.Role;
import org.hu.vhr.service.MenuService;
import org.hu.vhr.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限组
 */
@RestController
@RequestMapping("/system/basic/perms")
public class PermissionController {
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;

    @GetMapping("/")
    public List<Role> getAllRoles(){
        return roleService.getAllRoles();
    }

    @GetMapping("/menus")
    public List<Menu> getAllMenus(){
        return menuService.getAllMenus();
    }
    @GetMapping("/mids/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid){
        return menuService.getMidsByRid(rid);
    }
    @PostMapping("/")
    public RespBean insertRoles(@RequestBody Role role){
        if(roleService.insertRoles(role)!=0){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @PutMapping("/")
    public RespBean updateRoles(Integer rid,Integer[] mids){
        if(menuService.updateRoles(rid,mids)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteRole(@PathVariable Integer id){
        if(roleService.deleteRole(id)==1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除成功");
    }
}
